'''
请实现一个函数按照之字形打印二叉树，即第一行按照从左到右的顺序打印，第二层按照从右至左的顺序打印，第三行按照从左到右的顺序打印，其他行以此类推。

解题思路：按行遍历然后奇数正想添加偶数反向添加
'''
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def Print(self, pRoot):
        if not pRoot:return []
        cur = [pRoot]
        res = []
        while cur:
            nextStack = []
            res1 = []
            for x in cur:
                res1.append(x.val)
                if x.left: nextStack.append(x.left)
                if x.right: nextStack.append(x.right)
            res.append(res1)
            cur = nextStack
        result = []
        i = 0
        for i in range(len(res)):
            if (i+1)%2 == 0:
                result.append(res[i][::-1])
            else:
                result.append(res[i])
        return result